package com.chenjiagou.ordertickets.controller;

import com.chenjiagou.ordertickets.entity.Flight;
import com.chenjiagou.ordertickets.pojo.PageResult;
import com.chenjiagou.ordertickets.service.FlightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/planeTicket")
@Slf4j
public class PlaneTicketsController {
    @Resource
    private FlightService flightService;
    @RequestMapping("/index")
    public String index(){
        log.info("跳转到机票页面");
        return "index";
    }
    @RequestMapping("/search")
    public String query(
            @RequestParam String departureCity,
            @RequestParam String arrivalCity,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date departureDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date returnDate,
            @RequestParam(required = false, defaultValue = "false") boolean roundTrip,
            @RequestParam(required = false, defaultValue = "1") int outPage,
            @RequestParam(required = false, defaultValue = "1") int returnPage,
            Model model){
        int pageSize = 5;
        log.info("出发城市：{}",departureCity);
        log.info("到达城市：{}",arrivalCity);
        log.info("出发日期：{}",departureDate);
       log.info("返回日期：{}",returnDate);
       log.info("是否往返：{}",roundTrip);
        log.info("跳转到机票查询页面");
        if(roundTrip==true){
            PageResult<Flight> returnFlights = flightService.searchFlights(arrivalCity, departureCity, returnDate, returnPage, pageSize);
            model.addAttribute("returnFlights", returnFlights.getContent());
            model.addAttribute("returnPage", returnPage);
            model.addAttribute("returnTotalPages", returnFlights.getTotalPages());
            log.info("返回的航班信息为：{}",returnFlights.getContent());
        }
        PageResult<Flight> outboundFlights = flightService.searchFlights(departureCity, arrivalCity, departureDate, outPage, pageSize);
        model.addAttribute("outboundFlights", outboundFlights.getContent());
        model.addAttribute("outPage", outPage);
        model.addAttribute("outTotalPages", outboundFlights.getTotalPages());
        model.addAttribute("departureCity", departureCity);
        model.addAttribute("arrivalCity", arrivalCity);
        model.addAttribute("departureDate", departureDate);
        model.addAttribute("returnDate", returnDate);
        model.addAttribute("roundTrip", roundTrip);
            log.info("出发的航班信息为：{}",outboundFlights.getContent());
        return "search";
    }
}
